<template>
  <div>
    <div class="classify_header">
      <img src="http://localhost:8080/image/logo.png" width="100%" />
      <img src="http://localhost:8080/image/logo2.png" width="100%" />
    </div>
    <div class="classify_box">
      <!-- 左侧 -->
      <div class="classify_box_left">
        <div class="classify_box_left_box">
          <div
            v-for="(item,index) in category"
            @click="oneClick(index)"
            :key="index"
            :class="select == index ? 'active' : 'classify_box_left_box div' "
            ref
          >{{item.anchor_point}}</div>
        </div>
      </div>
      <!-- 右侧 -->
      <div class="classify_box_right" ref="right" @touchstart="onMouseEnter">
        <ClassifyGoods
          v-for="(item,index) in category"
          :key="index"
          :item="item"
          :index="index"
          ref="twonav"
        />
      </div>
    </div>
    <!-- 底部 -->
    <div class="classify_footer">
      <div class="classify_footer_one" @click="onclickShow">
        <van-icon name="shopping-cart-o" :badge="cart.length" size="30" />
      </div>
      <div class="classify_price">总价:{{this.$store.state.total}}</div>
    </div>
    <!-- 购物车详情--遮罩层 -->
    <van-overlay style="width:100%;height:676px;" :show="show" @click="show = false">
      <div class="wrapper" @click.stop>
        <div class="block">
          <div class="overlay_xuanz">
            <div class="overlay_xuanz_one">
              <input type="checkbox" :checked="this.$store.state.allChecked" @change="checkedAll" />全选
            </div>
            <div class="overlay_xuanz_two" @click="back">
              <van-icon name="cross" size="20" />
            </div>
          </div>
          <div class="overlay_list" v-for="(item,index) in cart" :key="index">
            <div class="overlay_check">
              <input type="checkbox" :checked="item.checked" @change="changeChecked(item)" />
            </div>
            <div class="overlay_left">
              <img :src="item.item.comm_image" alt />
            </div>
            <div class="overlay_center">
              <div class="name">{{item.item.comm_title}}</div>
              <div class="price">￥{{item.item.comm_price}}</div>
              <div class="jiajian">
                <button @click="sub(item)">-</button>
                {{item.count}}
                <button @click="add(item)">+</button>
              </div>
            </div>
            <div class="overlay_right">
              <button @click="del(index)">删除</button>
            </div>
          </div>
          <div v-show="this.$store.state.isShow">
            <p>购物车现在为空哦~</p>
            <p>请尽情选购呢！</p>
          </div>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
import ClassifyGoods from "../components/ClassifyGoods";
export default {
  components: {
    ClassifyGoods
  },
  data() {
    return {
      show: false
    };
  },
  mounted() {
    if (this.$store.state.cart.length == 0) {
      this.$store.state.isShow = true;
    } else {
      this.$store.state.isShow = false;
    }
    this.$store.state.total = 0;
    this.$store.state.cart.map(ele => {
      ele.checked = true;
      if (ele.checked) {
        this.$store.state.allChecked = true;
        this.$store.state.total += ele.count * ele.item.comm_price;
      }
    });
    this.$store.dispatch("GET_CATEGORY_DATA");
    // 给右侧加一个监听事件
    this.$refs.right.addEventListener("scroll", this.onScroll, true);
  },
  computed: {
    category() {
      return this.$store.state.category;
    },
    select() {
      return this.$store.state.select;
    },
    cart() {
      return this.$store.state.cart;
    }
  },
  methods: {
    // 点击上面的复选框
    changeChecked(item) {
      this.$store.dispatch("CHANGECHECKED", item);
    },
    checkedAll() {
      this.$store.dispatch("ChECKALL");
    },
    // 隐藏购物车详情--遮罩层
    back() {
      this.show = false;
    },
    // 删除数据
    del(index) {
      this.$store.dispatch("DELETE_DATA", index);
    },
    // 加数据
    add(item) {
      this.$store.dispatch("ADD_DATA", item);
    },
    // 减
    sub(item) {
      this.$store.dispatch("SUB_DATA", item);
    },
    onclickShow() {
      this.show = !this.show;
    },
    oneClick(index) {
      this.$store.dispatch("GET_CATEGORY_INDEX", index);
      var divElement = this.$refs.twonav[index].$el;
      divElement.scrollIntoView({ behavior: "smooth", block: "start" });
      this.$refs.right.removeEventListener("scroll", this.onScroll, true);
    },
    onScroll(event) {
      // 获取右侧的滚动高度
      var elementScrollTop = event.target.scrollTop;
      // 获取右侧的所有块
      var divElements = this.$refs.twonav;
      // 判断index
      for (let index = 0; index < divElements.length; index++) {
        var currentElement = divElements[index];
        var nextElement = divElements[index + 1];
        if (
          elementScrollTop > currentElement.$el.offsetTop &&
          elementScrollTop < nextElement.$el.offsetTop
        ) {
          this.$store.dispatch("GET_CATEGORY_INDEX", index);
        }
      }
    },
    onMouseEnter() {
      this.$refs.right.addEventListener("scroll", this.onScroll, true);
    }
  }
};
</script>

<style scoped>
.classify_header {
  position: fixed;
  top: 0;
  background-color: #fff;
  z-index: 7;
}
.active {
  color: red !important;
  border-left: 3px solid red !important;
}
.classify_box {
  width: 100%;
  height: 617px;
  display: flex;
  margin-top: 11rem;
  margin-bottom: 3.2rem;
}
.classify_box_left {
  width: 35%;
  height: 100%;
  background-color: white;
  overflow: hidden;
  overflow-x: hidden;
  overflow-y: scroll;
  border-right: 1px solid #eeeeee;
}
.classify_box_left_box {
  width: 100%;
  height: 100%;
}
.classify_box_left_box div {
  width: 100%;
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 16px;
  border-left: 3px solid white;
  color: #333333;
}
.classify_box_right {
  width: 65%;
  height: 100%;
  background-color: white;
  overflow: hidden;
  overflow-x: hidden;
  overflow-y: scroll;
}
.classify_footer {
  width: 100%;
  height: 50px;
  background-color: white;
  border-top: 1px solid #eeeeee;
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 10000;
}
.classify_price {
  margin-left: 90px;
  margin-top: -15px;
}
.classify_footer_one {
  width: 60px;
  height: 60px;
  background-color: #eeeeee;
  border-radius: 50%;
  margin-top: -30px;
  margin-left: 10px;
  text-align: center;
}
.classify_footer_one i {
  margin-top: 15px;
}
.wrapper {
  height: 100%;
}

.block {
  width: 100%;
  height: 50%;
  position: absolute;
  bottom: 60px;
  background-color: #f6f6f6f6;
  overflow: scroll;
}
.overlay_xuanz {
  width: 100%;
  height: 30px;
  display: flex;
  justify-content: space-between;
  line-height: 30px;
  background-color: white;
}
.overlay_xuanz_one {
  height: 30px;
}
.overlay_xuanz_one input {
  width: 20px;
  height: 20px;
  border: 1px solid #eeeeee;
  background-color: #eeeeee !important;
  margin-left: 10px;
  margin-top: 5px;
}
.overlay_xuanz_two i {
  margin-top: 5px;
  margin-right: 10px;
}
.overlay_list {
  width: 100%;
  height: 100px;
  display: flex;
  margin-top: 10px;
  background-color: white;
}
.overlay_check {
  flex: 0.1;
}
.overlay_check input {
  width: 20px;
  height: 20px;
  border: 1px solid #eeeeee;
  background-color: #eeeeee !important;
  margin-left: 10px;
  margin-top: 40px;
}
.overlay_left {
  flex: 1;
}
.overlay_left img {
  width: 80%;
  height: 90px;
  margin-left: 5%;
  margin-top: 5px;
}
.overlay_center {
  flex: 1.5;
}
.overlay_center .name {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-indent: 20px;
  margin-top: 10px;
}
.overlay_center .price {
  text-indent: 20px;
  font-size: 20px;
  color: red;
}
.overlay_center .jiajian {
  width: 100%;
  height: 50px;
  text-indent: 80px;
  margin-top: -25px;
}
.overlay_center .jiajian button {
  width: 15%;
  height: 30px;
  background-color: white;
  border: 1px solid #eeeeee;
}
.overlay_right {
  flex: 1;
}
.overlay_right button {
  width: 90px;
  height: 40px;
  background-color: brown;
  color: white;
  border: 1px solid gray;
  margin-top: 25px;
  margin-left: 15px;
}
</style>